import * as actionTypes from "./constants";

import {
  getBannerTop,
  getPersonalized,
  getAlbumBanners,
  getToplist,
  getPlaylistId,
  getSinger,
  getHotDjprogram,
  getAlbumNewest,
  getComment,
} from "@/services/recommend";

import { getNewHotComment } from "@/services/comment";

import { myUserDetailFunc } from "@/services/myevent";

const BannerTopAction = (res) => ({
  type: actionTypes.BNNAER_TOP,
  banner: res.banners,
});

const GetPersonalized = (res) => ({
  type: actionTypes.PERSONALIZED,
  personalized: res.result,
});

const GetAlbumBannerAction = (res) => ({
  type: actionTypes.ALBUM_BANNER,
  albumBanners: res.weekData,
});

const GetTopListAction = (res) => ({
  type: actionTypes.TOPLIST,
  toplists: res.list,
});

const GetTopListIdAction = (res) => ({
  type: actionTypes.TOPUPLIST,
  topUpList: res.playlist,
});
const GetTopListIdActionTwo = (res) => ({
  type: actionTypes.TOPNEWLIST,
  topNewList: res.playlist,
});
const GetTopListIdActionThree = (res) => ({
  type: actionTypes.TOPORIGINLIST,
  topOriginList: res.playlist,
});

const GetSingerListAction = (res) => ({
  type: actionTypes.GETSINGERLISTACTION,
  singerList: res.artists,
});
const GetHotDjProgramAction = (res) => ({
  type: actionTypes.GETHOTDJPROGRAMACTION,
  hotDjprogramList: res.result,
});

const GetTopListXQ = (res) => ({
  type: actionTypes.TOPLISTXQ,
  toplistXq: res.playlist,
});

const albumNewest = (res) => ({
  type: actionTypes.GETALBUMNEWEST,
  albumNewestInfo: res.albums,
});

const myUserDetail = (res) => ({
  type: actionTypes.MYUSERDETAILINFO,
  myUserDetailInfo: res,
});

const getComments = (res) => ({
  type: actionTypes.GETCOMMENT,
  getCommentInfo: res,
});

const getNewComments = (res) => ({
  type: actionTypes.GETNEWCOMMENT,
  newComments: res,
});

export const getBanner = () => {
  return (dispatch) => {
    getBannerTop().then((res) => {
      dispatch(BannerTopAction(res));
    });
  };
};

export const getPersonalize = () => {
  return (dispatch) => {
    getPersonalized().then((res) => {
      dispatch(GetPersonalized(res));
    });
  };
};

export const getAlbumBannerAction = (page) => {
  return (dispatch) => {
    getAlbumBanners(10, (page - 1) * 10).then((res) => {
      dispatch(GetAlbumBannerAction(res));
    });
  };
};

export const getTopList = () => {
  return (dispatch) => {
    getToplist().then((res) => {
      dispatch(GetTopListAction(res));
    });
  };
};
export const getTopListXq = (id) => {
  return (dispatch) => {
    if (id == undefined) {
      getPlaylistId(19723756).then((res) => {
        dispatch(GetTopListXQ(res));
      });
    } else {
      getPlaylistId(id).then((res) => {
        dispatch(GetTopListXQ(res));
      });
    }
  };
};

export const getTopListIdNew = (id) => {
  return (dispatch) => {
    getPlaylistId(id).then((res) => {
      switch (id) {
        case 19723756:
          dispatch(GetTopListIdAction(res));
          break;
        case 3779629:
          dispatch(GetTopListIdActionTwo(res));
          break;
        case 2884035:
          dispatch(GetTopListIdActionThree(res));
          break;
        default:
          break;
      }
    });
  };
};

export const getSingerList = (limit, offset) => {
  return (dispatch) => {
    getSinger(limit, offset).then((res) => {
      return dispatch(GetSingerListAction(res));
    });
  };
};

export const getHotDjProgram = () => {
  return (dispatch) => {
    getHotDjprogram().then((res) => {
      dispatch(GetHotDjProgramAction(res));
    });
  };
};

export const getAlbumNewestAction = () => {
  return (dispatch) => {
    getAlbumNewest().then((res) => {
      dispatch(albumNewest(res));
    });
  };
};
export const myUserDetailAction = (uid) => {
  return (dispatch) => {
    myUserDetailFunc(uid).then((res) => {
      dispatch(myUserDetail(res));
    });
  };
};

export const getCommentAction = (id, limit, offset, before) => {
  return (dispatch) => {
    if (id == undefined) {
      getComment(19723756, limit, offset, before).then((res) => {
        dispatch(getComments(res));
      });
    } else {
      getComment(id, limit, offset, before).then((res) => {
        dispatch(getComments(res));
      });
    }
  };
};

export const getNewCommentAction = (
  id,
  type,
  pageNo,
  pageSize,
  sortType,
  cursor
) => {
  return (dispatch) => {
    if (id == undefined) {
      getNewHotComment(19723756, type, pageNo, pageSize, sortType, cursor).then(
        (res) => {
          dispatch(getNewComments(res));
        }
      );
    } else {
      getNewHotComment(id, type, pageNo, pageSize, sortType, cursor).then(
        (res) => {
          dispatch(getNewComments(res));
        }
      );
    }
  };
};
